package emuLoader.gui.javaFx;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import nayasis.common.device.input.GamePadEvent;
import nayasis.common.device.input.GamePadEventHandler;
import nayasis.common.device.input.GamePadEventManager;
import nayasis.common.log.NLogger;
import nayasis.common.model.NData;
import nayasis.common.model.NMultiData;
import nayasis.common.ui.javaFx.controller.NfxTable;
import nayasis.common.util.StringUtil;
import emuLoader.biz.GameData;

public class EmuLoader implements Initializable {
	
	@FXML public  TextField    inputSearchGame; 
	@FXML public  Label        labelStatus; 
	@FXML private TableView<?> tableGameListRaw;
	
	public NfxTable tableGameList;
	
	@Override
	public void initialize( URL url, ResourceBundle resourceBundle ) {

	    tableGameList = new NfxTable( tableGameListRaw, "title", "playtime", "category", "year", "manufacturer" );

	    tableGameList.addEventKeyup( new EventHandler<KeyEvent>() {
            public void handle(KeyEvent event) {
                NLogger.debug.println( "#B : row[@], col[@]", tableGameList.getFocusedRowIndex(), tableGameList.getFocusedColumnIndex() );
                NLogger.debug.println( "#B : [@] @", event.isAltDown(), event.getCode() );              
            }
        });

	    tableGameList.addEventClick( new EventHandler<MouseEvent>() {
            public void handle(MouseEvent event) {
                NLogger.debug.println( "#C : row[@], col[@]", tableGameList.getFocusedRowIndex(), tableGameList.getFocusedColumnIndex() );
                NLogger.debug.println( "#C : [@,@]", event.getX(), event.getY() );              
                
            }
        });
	        
        GamePadEventManager.getInstance().addEvent( new GamePadEventHandler() {
            public void handle( GamePadEvent event ) {
                
                tableGameList.getInstance().requestFocus();
                
                int focusedRowIndex = tableGameList.getFocusedRowIndex();
                
                switch( event.getPovValue() ) {
                    case UP    : focusedRowIndex--;     break;
                    case DOWN  : focusedRowIndex++;     break;
                    case RIGHT : focusedRowIndex += 10; break;
                    case LEFT  : focusedRowIndex -= 10; break;
                    default    : return;
                }
                
                tableGameList.select( focusedRowIndex );
                
            }
        });
	    
	    retrieveGameList( null );
		
	}

	private void retrieveGameList( String emulKind ) {

	    GameData biz = new GameData();
	        
	    NMultiData romPath = biz.retrieveGameInfo( emulKind );

	    tableGameList.bind( romPath );

	    printStatus( "Done game search (@ count)", tableGameList.getRowSize() );
	    
	}

	private void printStatus( String format, Object... params ) {
	    
	    labelStatus.setText( StringUtil.format(format, params) );
	}
	
	@FXML
	private void searchGame( ActionEvent event ) {

		NLogger.debug.println( "[@] inputed !!", inputSearchGame.getText() );

//		tableGameList.addData( inputSearchGame.getText() );
		
//		ObservableList<?> columns = tableGameList.getColumns();
//		
//		TableColumn column = (TableColumn) columns.get(0);
//		
//		Logger.debug.println( columns.size() );
//		Logger.debug.println( column.getText() );
		
		NData row = new NData();
		row.put("title", inputSearchGame.getText() );
		row.put("category", "A");
		
		tableGameList.addRow( row );
		
//		boolean confirm = Dialog.confirm( "입력내용 : @\n\n\n\n\n1\n\n\n\n\n3\n\n\n\n\n5", inputSearchGame.getText() );
//
//		Dialog.alert( "입력결과 : @", confirm );
		
	}
	
}
